{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import sklearn \n",
    "import openpyxl\n",
    "import warnings\n",
    "from functools import reduce\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_1 = pd.read_excel(r\"C:\\df_analysis_1.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_1.columns.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_1 = df_1.groupby('si_gun_gu').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import statsmodels.api as sm\n",
    "from matplotlib.gridspec import GridSpec\n",
    "\n",
    "# Function to fit model and get coefficient and p-value\n",
    "def get_model_params(x, y, var_name):\n",
    "    X = sm.add_constant(x)  # Adds a constant term to the predictor\n",
    "    model = sm.OLS(y, X).fit()\n",
    "    coeff = model.params[var_name]\n",
    "    p_value = model.pvalues[var_name]\n",
    "    return coeff, p_value\n",
    "\n",
    "# Create figure and GridSpec\n",
    "fig = plt.figure(figsize=(21, 14))\n",
    "gs = GridSpec(2, 4, width_ratios=[1, 1, 0.96, 0.08], height_ratios=[1, 1])\n",
    "\n",
    "# Plot 1: EVI vs. Distrust\n",
    "ax1 = fig.add_subplot(gs[0, 0])\n",
    "coeff, p_value = get_model_params(df_1['EVI'], df_1['Distrust'], 'EVI')\n",
    "scatter1 = ax1.scatter(df_1['EVI'], df_1['Distrust'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Loneliness'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='EVI', y='Distrust', data=df_1, scatter=False, color='red', ax=ax1)\n",
    "ax1.set_xlabel('EVI', size=22)\n",
    "ax1.set_ylabel('Distrust', size=22)\n",
    "ax1.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax1.transAxes, fontsize=20, color='blue')\n",
    "ax1.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax1.transAxes, fontsize=20, color='blue')\n",
    "ax1.grid(True)\n",
    "ax1.set_title('(a) Distrust (EVI)', size=26, pad=25)\n",
    "ax1.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 2: EVI vs. Exploitation\n",
    "ax2 = fig.add_subplot(gs[0, 1])\n",
    "coeff, p_value = get_model_params(df_1['EVI'], df_1['Exploitation'], 'EVI')\n",
    "scatter2 = ax2.scatter(df_1['EVI'], df_1['Exploitation'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Loneliness'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='EVI', y='Exploitation', data=df_1, scatter=False, color='red', ax=ax2)\n",
    "ax2.set_xlabel('EVI', size=22)\n",
    "ax2.set_ylabel('Exploitation', size=22)\n",
    "ax2.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax2.transAxes, fontsize=20, color='blue')\n",
    "ax2.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax2.transAxes, fontsize=20, color='blue')\n",
    "ax2.grid(True)\n",
    "ax2.set_title('(b) Exploitation (EVI)', size=26, pad=25)\n",
    "ax2.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 3: EVI vs. Indifference\n",
    "ax3 = fig.add_subplot(gs[0, 2])\n",
    "coeff, p_value = get_model_params(df_1['EVI'], df_1['Indifference'], 'EVI')\n",
    "scatter3 = ax3.scatter(df_1['EVI'], df_1['Indifference'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Loneliness'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='EVI', y='Indifference', data=df_1, scatter=False, color='red', ax=ax3)\n",
    "ax3.set_xlabel('EVI', size=22)\n",
    "ax3.set_ylabel('Indifference', size=22)\n",
    "ax3.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax3.transAxes, fontsize=20, color='blue')\n",
    "ax3.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax3.transAxes, fontsize=20, color='blue')\n",
    "ax3.grid(True)\n",
    "ax3.set_title('(c) Indifference (EVI)', size=26, pad=25)\n",
    "ax3.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 4: NDVI vs. Distrust\n",
    "ax4 = fig.add_subplot(gs[1, 0])\n",
    "coeff, p_value = get_model_params(df_1['NDVI'], df_1['Distrust'], 'NDVI')\n",
    "scatter4 = ax4.scatter(df_1['NDVI'], df_1['Distrust'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Loneliness'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='NDVI', y='Distrust', data=df_1, scatter=False, color='red', ax=ax4)\n",
    "ax4.set_xlabel('NDVI', size=22)\n",
    "ax4.set_ylabel('Distrust', size=22)\n",
    "ax4.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax4.transAxes, fontsize=20, color='blue')\n",
    "ax4.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax4.transAxes, fontsize=20, color='blue')\n",
    "ax4.grid(True)\n",
    "ax4.set_title('(d) Distrust (NDVI)', size=26, pad=25)\n",
    "ax4.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 5: NDVI vs. Exploitation\n",
    "ax5 = fig.add_subplot(gs[1, 1])\n",
    "coeff, p_value = get_model_params(df_1['NDVI'], df_1['Exploitation'], 'NDVI')\n",
    "scatter5 = ax5.scatter(df_1['NDVI'], df_1['Exploitation'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Loneliness'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='NDVI', y='Exploitation', data=df_1, scatter=False, color='red', ax=ax5)\n",
    "ax5.set_xlabel('NDVI', size=22)\n",
    "ax5.set_ylabel('Exploitation', size=22)\n",
    "ax5.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax5.transAxes, fontsize=20, color='blue')\n",
    "ax5.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax5.transAxes, fontsize=20, color='blue')\n",
    "ax5.grid(True)\n",
    "ax5.set_title('(e) Exploitation (NDVI)', size=26, pad=25)\n",
    "ax5.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 6: NDVI vs. Indifference\n",
    "ax6 = fig.add_subplot(gs[1, 2])\n",
    "coeff, p_value = get_model_params(df_1['NDVI'], df_1['Indifference'], 'NDVI')\n",
    "scatter6 = ax6.scatter(df_1['NDVI'], df_1['Indifference'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Loneliness'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='NDVI', y='Indifference', data=df_1, scatter=False, color='red', ax=ax6)\n",
    "ax6.set_xlabel('NDVI', size=22)\n",
    "ax6.set_ylabel('Indifference', size=22)\n",
    "ax6.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax6.transAxes, fontsize=20, color='blue')\n",
    "ax6.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax6.transAxes, fontsize=20, color='blue')\n",
    "ax6.grid(True)\n",
    "ax6.set_title('(f) Indifference (NDVI)', size=26, pad=25)\n",
    "ax6.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Add colorbar\n",
    "cbar_ax = fig.add_axes([0.8, 0.05, 0.02, 0.62])  # [left, bottom, width, height]\n",
    "cbar = fig.colorbar(scatter6, cax=cbar_ax)\n",
    "cbar.ax.tick_params(labelsize=16)\n",
    "cbar.set_label(label='Loneliness', size=26)\n",
    "\n",
    "# Add legend for point sizes\n",
    "handles, labels = scatter1.legend_elements(prop=\"sizes\", alpha=0.6)\n",
    "legend2 = fig.legend(handles, labels, loc=\"center right\", bbox_to_anchor=(0.87, 0.82), title=\"Loneliness\", fontsize=16, title_fontsize=20)\n",
    "\n",
    "plt.tight_layout(rect=[0, 0, 0.85, 1])  # Adjust the main plot area to make room for the colorbar\n",
    "plt.show()\n",
    "\n",
    "#plt.savefig(r'C:\\Users\\Han\\Desktop\\document\\불평등 연구\\주제36_녹지불평등\\graph\\figure_2.tiff', dpi=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import statsmodels.api as sm\n",
    "from matplotlib.gridspec import GridSpec\n",
    "\n",
    "# Function to fit model and get coefficient and p-value\n",
    "def get_model_params(x, y, var_name):\n",
    "    X = sm.add_constant(x)  # Adds a constant term to the predictor\n",
    "    model = sm.OLS(y, X).fit()\n",
    "    coeff = model.params[var_name]\n",
    "    p_value = model.pvalues[var_name]\n",
    "    return coeff, p_value\n",
    "\n",
    "# Create figure and GridSpec\n",
    "fig = plt.figure(figsize=(21, 14))\n",
    "gs = GridSpec(2, 4, width_ratios=[1, 1, 0.96, 0.08], height_ratios=[1, 1])\n",
    "\n",
    "# Plot 1: EVI vs. Distrust\n",
    "ax1 = fig.add_subplot(gs[0, 0])\n",
    "coeff, p_value = get_model_params(df_1['EVI'], df_1['Distrust'], 'EVI')\n",
    "scatter1 = ax1.scatter(df_1['EVI'], df_1['Distrust'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Distrust'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='EVI', y='Distrust', data=df_1, scatter=False, color='red', ax=ax1)\n",
    "ax1.set_xlabel('EVI', size=22)\n",
    "ax1.set_ylabel('Distrust', size=22)\n",
    "ax1.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax1.transAxes, fontsize=20, color='blue')\n",
    "ax1.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax1.transAxes, fontsize=20, color='blue')\n",
    "ax1.grid(True)\n",
    "ax1.set_title('(a) Distrust (EVI)', size=26, pad=25)\n",
    "ax1.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 2: EVI vs. Exploitation\n",
    "ax2 = fig.add_subplot(gs[0, 1])\n",
    "coeff, p_value = get_model_params(df_1['EVI'], df_1['Exploitation'], 'EVI')\n",
    "scatter2 = ax2.scatter(df_1['EVI'], df_1['Exploitation'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Exploitation'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='EVI', y='Exploitation', data=df_1, scatter=False, color='red', ax=ax2)\n",
    "ax2.set_xlabel('EVI', size=22)\n",
    "ax2.set_ylabel('Exploitation', size=22)\n",
    "ax2.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax2.transAxes, fontsize=20, color='blue')\n",
    "ax2.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax2.transAxes, fontsize=20, color='blue')\n",
    "ax2.grid(True)\n",
    "ax2.set_title('(b) Exploitation (EVI)', size=26, pad=25)\n",
    "ax2.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 3: EVI vs. Indifference\n",
    "ax3 = fig.add_subplot(gs[0, 2])\n",
    "coeff, p_value = get_model_params(df_1['EVI'], df_1['Indifference'], 'EVI')\n",
    "scatter3 = ax3.scatter(df_1['EVI'], df_1['Indifference'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Indifference'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='EVI', y='Indifference', data=df_1, scatter=False, color='red', ax=ax3)\n",
    "ax3.set_xlabel('EVI', size=22)\n",
    "ax3.set_ylabel('Indifference', size=22)\n",
    "ax3.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax3.transAxes, fontsize=20, color='blue')\n",
    "ax3.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax3.transAxes, fontsize=20, color='blue')\n",
    "ax3.grid(True)\n",
    "ax3.set_title('(c) Indifference (EVI)', size=26, pad=25)\n",
    "ax3.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 4: NDVI vs. Distrust\n",
    "ax4 = fig.add_subplot(gs[1, 0])\n",
    "coeff, p_value = get_model_params(df_1['NDVI'], df_1['Distrust'], 'NDVI')\n",
    "scatter4 = ax4.scatter(df_1['NDVI'], df_1['Distrust'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Distrust'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='NDVI', y='Distrust', data=df_1, scatter=False, color='red', ax=ax4)\n",
    "ax4.set_xlabel('NDVI', size=22)\n",
    "ax4.set_ylabel('Distrust', size=22)\n",
    "ax4.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax4.transAxes, fontsize=20, color='blue')\n",
    "ax4.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax4.transAxes, fontsize=20, color='blue')\n",
    "ax4.grid(True)\n",
    "ax4.set_title('(d) Distrust (NDVI)', size=26, pad=25)\n",
    "ax4.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 5: NDVI vs. Exploitation\n",
    "ax5 = fig.add_subplot(gs[1, 1])\n",
    "coeff, p_value = get_model_params(df_1['NDVI'], df_1['Exploitation'], 'NDVI')\n",
    "scatter5 = ax5.scatter(df_1['NDVI'], df_1['Exploitation'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Exploitation'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='NDVI', y='Exploitation', data=df_1, scatter=False, color='red', ax=ax5)\n",
    "ax5.set_xlabel('NDVI', size=22)\n",
    "ax5.set_ylabel('Exploitation', size=22)\n",
    "ax5.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax5.transAxes, fontsize=20, color='blue')\n",
    "ax5.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax5.transAxes, fontsize=20, color='blue')\n",
    "ax5.grid(True)\n",
    "ax5.set_title('(e) Exploitation (NDVI)', size=26, pad=25)\n",
    "ax5.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Plot 6: NDVI vs. Indifference\n",
    "ax6 = fig.add_subplot(gs[1, 2])\n",
    "coeff, p_value = get_model_params(df_1['NDVI'], df_1['Indifference'], 'NDVI')\n",
    "scatter6 = ax6.scatter(df_1['NDVI'], df_1['Indifference'], s=df_1['Loneliness']*20,\n",
    "                       c=df_1['Indifference'], cmap='Accent', alpha=0.5)\n",
    "sns.regplot(x='NDVI', y='Indifference', data=df_1, scatter=False, color='red', ax=ax6)\n",
    "ax6.set_xlabel('NDVI', size=22)\n",
    "ax6.set_ylabel('Indifference', size=22)\n",
    "ax6.text(0.05, 0.9, f'Coef: {coeff:.3f}', transform=ax6.transAxes, fontsize=20, color='blue')\n",
    "ax6.text(0.05, 0.85, f'p-value: {p_value:.3f}', transform=ax6.transAxes, fontsize=20, color='blue')\n",
    "ax6.grid(True)\n",
    "ax6.set_title('(f) Indifference (NDVI)', size=26, pad=25)\n",
    "ax6.tick_params(axis='both', which='major', labelsize=18)\n",
    "\n",
    "# Add colorbar\n",
    "cbar_ax = fig.add_axes([0.8, 0.05, 0.02, 0.62])  # [left, bottom, width, height]\n",
    "cbar = fig.colorbar(scatter6, cax=cbar_ax)\n",
    "cbar.ax.tick_params(labelsize=16)\n",
    "cbar.set_label(label='Perceptions of Fragmentation', size=26)\n",
    "\n",
    "# Add legend for point sizes\n",
    "handles, labels = scatter1.legend_elements(prop=\"sizes\", alpha=0.6)\n",
    "legend2 = fig.legend(handles, labels, loc=\"center right\", bbox_to_anchor=(0.87, 0.82), title=\"Loneliness\", fontsize=16, title_fontsize=20)\n",
    "\n",
    "plt.tight_layout(rect=[0, 0, 0.85, 1])  # Adjust the main plot area to make room for the colorbar\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
